home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python-1.4 / Lib / regexp.py < prev    next >
Text File  |  1998-06-24  |  706b  |  33 lines

  1. # Provide backward compatibility for module "regexp" using "regex".
  2.  
  3. import regex
  4. from regex_syntax import *
  5.  
  6. class Prog:
  7.     def __init__(self, pat):
  8.         save_syntax = regex.set_syntax(RE_SYNTAX_AWK)
  9.         try:
  10.             self.prog = regex.compile(pat)
  11.         finally:
  12.             xxx = regex.set_syntax(save_syntax)
  13.     def match(self, str, offset = 0):
  14.         if self.prog.search(str, offset) < 0:
  15.             return ()
  16.         regs = self.prog.regs
  17.         i = len(regs)
  18.         while i > 0 and regs[i-1] == (-1, -1):
  19.             i = i-1
  20.         return regs[:i]
  21.  
  22. def compile(pat):
  23.     return Prog(pat)
  24.  
  25. cache_pat = None
  26. cache_prog = None
  27.  
  28. def match(pat, str):
  29.     global cache_pat, cache_prog
  30.     if pat <> cache_pat:
  31.         cache_pat, cache_prog = pat, compile(pat)
  32.     return cache_prog.match(str)
  33.